from requests import get
from requests.exceptions import *
from re import findall
from os import path, mkdir

""" 下载wget的源码 """
header = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}


def main(url):
    page = get(url, timeout=5, headers=header)
    print(f'stats_code is {page.status_code}')
    page.encoding = 'utf8'
    text = page.text
    urlHeader = 'https://eternallybored.org/misc/wget/src/'
    urlLast = findall(r'<a href="(.*?)">.*?</a>', text)
    if not path.exists('./wget'):
        mkdir('./wget')
    for u in urlLast:
        try:
            if not path.exists(f'./wget/{u}'):
                print(f'开始写入{u}', end='：')
                with open(f'./wget/{u}', 'wb') as f:
                    file = get(urlHeader + u, headers=header, timeout=10).content
                    f.write(file)
            else: print(f"文件{u}已存在")
        except FileNotFoundError:
            print('文件位置未知', end='||')
        except TimeoutError:
            print("连接超时", end='||')
        except OSError:
            print('文件名错误', end='||')
        if path.exists(f'./wget/{u}'):
            print('写入成功')
        else:
            print('写入失败')

    return True


if __name__ == '__main__':
    URL = 'https://eternallybored.org/misc/wget/src/'
    main(URL)
